package cn.migu.music.hangupsms.repository;

import java.time.LocalDateTime;
import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import cn.migu.music.hangupsms.common.entity.SmsTemplateSettings;

@Mapper
public interface SmsTemplateSettingsMapper {
    @Insert(value = "insert into t_migu_hangup_sms_template" +
                    "(template_id,template_content,change_time,apply_status,apply_time,delete_flag,create_time,update_time)" +
                    " values (#{settings.templateId},#{settings.templateContent},#{settings.changeTime}" +
                    ",#{settings.applyStatus},#{settings.applyTime},#{settings.deleteFlag},now(),now())")
    void insert(@Param("settings") SmsTemplateSettings settings);

    @Insert(value = "<script>\n" +
                    " insert into t_migu_hangup_sms_template\n" +
                    " (template_id,template_content,change_time,apply_status,apply_time,delete_flag,create_time,update_time)\n" +
                    " values \n" +
                    " <foreach collection=\"settingsList\" index=\"index\" item=\"item\" separator=\",\">\n" +
                    " (#{item.templateId},#{item.templateContent},#{item.changeTime},#{item.applyStatus},#{item.applyTime},#{item.deleteFlag},now(),now())\n" +
                    " </foreach>\n" +
                    " </script>")
    void batchInsert(@Param("settingsList") List<SmsTemplateSettings> settingsList);

    @Update(value = "<script> update t_migu_hangup_sms_template set \n" +
                    "  <if test=\"settings.templateContent != null\">\n" +
                    "  template_content=#{settings.templateContent},</if>\n" +
                    "  update_time=now()\n" +
                    " where template_id=#{settings.templateId} </script>")
    void updateTime(@Param("settings") SmsTemplateSettings settings);

    @Update(value = "update t_migu_hangup_sms_template" +
                    " set apply_status=#{applyStatus},apply_time=#{applyTime}" +
                    " where template_id=#{templateId}")
    void updateApplyStatus(@Param("templateId") String templateId,
                           @Param("applyStatus") int applyStatus,
                           @Param("applyTime") LocalDateTime applyTime);

    @Select(value = "select" +
                    " template_id,template_content,change_time,apply_status,apply_time,delete_flag,create_time,update_time" +
                    " from t_migu_hangup_sms_template " +
                    " where template_id=#{templateId}")
    SmsTemplateSettings get(@Param("templateId") String templateId);

    @Select(value = "select" +
                    " template_id,template_content,change_time,apply_status,apply_time,delete_flag,create_time,update_time" +
                    " from t_migu_hangup_sms_template " +
                    " where template_id like #{templateId}")
    List<SmsTemplateSettings> getLike(@Param("templateId") String templateId);

    @Select(value = "select" +
                    " template_id,template_content,change_time,apply_status,apply_time,delete_flag,create_time,update_time" +
                    " from t_migu_hangup_sms_template " +
                    " where delete_flag = 0")
    List<SmsTemplateSettings> getAllSettings();

    @Select(value = "select" +
                    " template_id,template_content,change_time,apply_status,apply_time,delete_flag,create_time,update_time" +
                    " from t_migu_hangup_sms_template " +
                    " where template_id like #{pattern}")
    List<SmsTemplateSettings> getByProvince(@Param("pattern") String pattern);

    @Delete(value = "delete from t_migu_hangup_sms_template where template_id=#{templateId}")
    void remove(@Param("templateId") String templateId);
}
